package com.cn.bulu.controller;

import cn.jsms.api.SendSMSResult;
import com.cn.bulu.dao.UploadDao;
import com.cn.bulu.utils.BuluResult;
import com.cn.bulu.utils.BuluWebResult;
import com.cn.bulu.utils.jpush.JSMSUtil;
import com.cn.bulu.utils.upload.UploadDemo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.*;

@Controller
@RequestMapping("upload")
public class UploadController {
	@Resource
	private UploadDao uploadDao;

	@RequestMapping("/getToken")
	@ResponseBody
	public BuluResult getToken(HttpServletResponse response) {
		response.setHeader("Access-Control-Allow-Origin", "*");  
	    response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE");  
	    response.setHeader("Access-Control-Max-Age", "3600");  
	    response.setHeader("Access-Control-Allow-Headers", "x-requested-with");
		UploadDemo demo = new UploadDemo();
		String upToken = demo.getUpToken();
		BuluResult buluResult = new BuluResult();
		buluResult.setStatus(0);
		buluResult.setData(upToken);
		System.out.println(upToken);
		return buluResult;
	}
	
	//web前端专用
	@RequestMapping("/getTokenWeb")
	@ResponseBody
	public BuluWebResult getTokenWeb(HttpServletResponse response) {
		response.setHeader("Access-Control-Allow-Origin", "*");  
	    response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE");  
	    response.setHeader("Access-Control-Max-Age", "3600");  
	    response.setHeader("Access-Control-Allow-Headers", "x-requested-with");
		UploadDemo demo = new UploadDemo();
		String upToken = demo.getUpToken();
		BuluWebResult buluResult = new BuluWebResult();
		buluResult.setUptoken(upToken);
		System.out.println(upToken);
		return buluResult;
	}

	// 根据手机号发送验证码
	/**
	 * @param sendType
	 *            sendType=0为注册验证码 sendType=1为工人点击完成发送给顾客验证码
	 * @param phone
	 * @return
	 */
	@RequestMapping("/sendMaByPhone")
	@ResponseBody
	public BuluResult sendMaByPhone(String phone, Integer sendType,HttpSession session) {
		System.out.println("================进入验证码生成环节====================");
		int number = (int) (Math.random() * (999999 - 100000 + 1)) + 100000;
		SendSMSResult smsResult = null;
		if (sendType == 0) {//下单版和师傅版
			System.out.println("=========================鹿客帮注册验证码0=======================："+String.valueOf(number));
			smsResult = JSMSUtil.sendTemplateSmsToUser(35990, phone, String.valueOf(number), "鹿客帮");
		} else if (sendType == 3){
			System.out.println("=========================鹿客家居注册验证码3=======================："+String.valueOf(number));
			smsResult = JSMSUtil.sendTemplateSmsToUser(35990, phone, String.valueOf(number),"鹿客家居");
		} else if (sendType == 4){
			System.out.println("=========================鹿客家居推广码4=======================："+String.valueOf(number));
			smsResult = JSMSUtil.sendTemplateSmsToUser(35990, phone, String.valueOf(number),"鹿客家居");
		} else {
			System.out.println("=========================工人完成任务 顾客给的验证码=======================："+String.valueOf(number));
			smsResult = JSMSUtil.sendTemplateSmsToUser(48983, phone, String.valueOf(number), "鹿客帮");
		}
		session.setAttribute(com.cn.bulu.constant.Const.SESSION_VERIFY_CODE, number);
		System.out.println(session.getAttribute(com.cn.bulu.constant.Const.SESSION_VERIFY_CODE));
		BuluResult buluResult = new BuluResult();
		if (smsResult.isResultOK()) {
			Map<String, Object> map = new HashMap<>();
			map.put("targetPhoneNum", phone);
			map.put("sendTime", new Date());
			map.put("code", number);
			map.put("codeType", sendType);
			uploadDao.saveCode(map);
			buluResult.setStatus(0);
			buluResult.setData(String.valueOf(number));
			return buluResult;
		} else {
			// 验证码发送失败
			buluResult.setStatus(1);
			buluResult.setMessage("验证码发送失败!!!");
			return buluResult;
		}
	}

	// 验证验证码
	@RequestMapping("checkNumber")
	@ResponseBody
	public BuluResult checkNumber(Integer number, String only) {
		// redis.auth("sd3a3lkf@23I.N8");
		// Date Time = new Date();
		// Long overTime = Time.getTime();
		// System.out.println(overTime);
		// System.out.println(redis.get(only+"beginTime"));
		// Long beginTime = Long.parseLong(redis.get(only+"beginTime"));
		// System.out.println(beginTime);
		BuluResult buluResult = new BuluResult();
		// System.out.println(overTime-beginTime);
		// if(overTime-beginTime<=12000){
		// Integer num = Integer.parseInt(redis.get(only+"number"));
		// if(number.equals(num)){
		// buluResult.setStatus(0);
		// }else{
		// buluResult.setStatus(2);
		// buluResult.setMessage("验证码有误!!!");
		// }
		// }else{
		// buluResult.setStatus(1);
		// buluResult.setMessage("验证码已失效,请重新验证!!!");
		// }
		return buluResult;
	}

	// 地址接口
	@RequestMapping("/selectAddress")
	@ResponseBody
	public BuluResult selectAddress() {
		// 查询省
		List<Map<String, Object>> province = uploadDao.selectSheng();
		for (int a = 0; a < province.size(); a++) {
			// 获取市
			List<Map<String, Object>> city = uploadDao.selectShi((String) province.get(a).get("province"));
			for (int b = 0; b < city.size(); b++) {
				// 获取市code
				Map<String, Object> code = uploadDao.selectCode(city.get(b).get("city"));
				Integer object = Integer.valueOf(code.get("code").toString());
				List<Map<String, Object>> chinaArea = uploadDao.getAllShiByS(object);
				List<String> area = new ArrayList<>();
				if ((Integer) code.get("level") == 1) {// 为直辖市
					for (int c = 0; c < chinaArea.size(); c++) {
						List<String> allShiByS = uploadDao.getAllShiByS2((Integer) chinaArea.get(c).get("code"));
						area.addAll(allShiByS);
					}
				} else {
					for (int c = 0; c < chinaArea.size(); c++) {
						area.add((String) chinaArea.get(c).get("name"));
					}
				}
				city.get(b).put("area", area);
			}
			province.get(a).put("citys", city);
		}
		BuluResult buluResult = new BuluResult();
		buluResult.setStatus(0);
		buluResult.setData(province);
		return buluResult;
	}

	// 根据市查询下面的区
	@RequestMapping("/selectArea")
	@ResponseBody
	public BuluResult selectArea(String shi) {
		List<Map<String, String>> list = uploadDao.selectArea(shi);
		Iterator<Map<String, String>> iterator = list.iterator();
		while (iterator.hasNext()) {
			Map<String, String> next = iterator.next();

			if ("其他区".equals((String) next.get("name"))) {
				iterator.remove();
			}
		}
		BuluResult buluResult = new BuluResult();
		buluResult.setStatus(0);
		buluResult.setData(list);
		return buluResult;
	}

}
